import React from 'react'
import { Switch, Redirect, Route } from 'react-router-dom'

const RouteView = (props) => {
    const { routes } = props
    return <Switch>
        {
            routes.map((item, index) => item.path ?
                <Route
                    key={index}
                    path={item.path}
                    render={(History) => beforeEnter(item, History) } />
                : <Redirect to="/login" key={index} {...item} />)
        }
    </Switch>
}

const beforeEnter = (item, History) => {
    return <item.component {...History} routes={item.children ? item.children : []}>
        <RouteView routes={item.children ? item.children : []} />
    </item.component>
}

export default RouteView